// <!--地区三级联动-->
// <script type="text/javascript" src="../js/area.js" ></script>
//     <script type="text/javascript">_init_area();</script>
// <div id="show"></div>
//     <script type="text/javascript">
// var Gid  = document.getElementById ;
// var showArea = function(){
//     Gid('show').innerHTML = "<h3>省" + Gid('province').value + " - 市" +
//         Gid('city').value + " - 县/区" +
//         Gid('district').value + "</h3>"
// }
// Gid('district').setAttribute('onchange','showArea()');
// </script>
;
function modifySubmitData() {
    if( $("#province").val() == "省份"){
        $("#province").val("");
    }
    if( $("#city").val() == "城市"){
        $("#city").val("");
    }
    if( $("#district").val() == "区域"){
        $("#district").val("");
    }
}
function validateForm() {
    return true;
}
function getOption() {
    if(loginUserType==5){//代理商只加服务城市的广告
        $('#province').css('display','none');
        $('#district').css('display','none');
    }else {
        _init_area();
    }
    var id = add_Obj.id;
    var parentId = add_Obj.pId;
    if(add_Obj.page_type == 2){
        if(add_Obj.opType == 1){
            $("#htmlTitle").html("新建广告栏目");
        }else if(add_Obj.opType == 2){
            $("#htmlTitle").html("编辑广告栏目");
        }
    }else if(add_Obj.page_type == 3){
        if(add_Obj.opType == 1){
            $("#htmlTitle").html("新建广告");
        }else if(add_Obj.opType == 2){
            $("#htmlTitle").html("编辑广告");
        }
    }
    $(".save").click(function () {
        var options = {
            dataType: 'json',
            beforeSerialize: modifySubmitData,
            beforeSubmit:validateForm,
            success: function (data) {
                if(add_Obj.page_type == 3) {//广告
                    if (data.msg== 'success') {
                        var _p_obj = {
                            page_type: 3,//列表类型，1 广告类，2广告栏目，3广告
                            pId: parentId,//上级ID
                            pId1: add_Obj.pId1,
                            province:'',//省
                            city: '',//市
                            district:''//区县
                        };
                        $.ajaxSetup({cache: false});
                        $(".mainDiv").load('htmlWeb/systemAdverListHtml', '', function () {
                            page_Obj = _p_obj;
                            $.getScript("../../by-webhtml/assets/js/system/system_adverList.js");
                        });
                    } else {
                        if (data.r != null) alert("广告编辑失败");
                        else alert("广告新建失败");
                    }

                }else {//广告栏目
                    if (data.msg== 'success') {
                        var _p_obj = {
                            page_type: 2,//列表类型，1 广告类，2广告栏目，3广告
                            pId: parentId,//上级ID
                            pId1: add_Obj.pId1,
                            province:'',//省
                            city: '',//市
                            district:''//区县
                        };
                        $.ajaxSetup({cache: false});
                        $(".mainDiv").load('htmlWeb/systemAdverListHtml', '', function () {
                            page_Obj = _p_obj;
                            $.getScript("../../by-webhtml/assets/js/system/system_adverList.js");
                        });
                    } else {
                        if (data.r != null) alert("广告栏目编辑失败");
                        else alert("广告新建栏目失败");
                    }
                }
            },
            error:function (data) {
                if(add_Obj.page_type == 3) {//广告
                    if (data.r != null) alert("广告编辑失败");
                    else alert("广告新建失败");
                    $.ajaxSetup({cache: false});
                }else {//广告栏目
                    if (data.r != null) alert("广告栏目编辑失败");
                    else alert("广告新建栏目失败");
                    $.ajaxSetup({cache: false});
                }
            }
        };
        $("#advForm").ajaxForm(options);
    });
    $("#cancel").click(function () {
        $.ajaxSetup({cache: false});
        if(add_Obj.page_type == 3) {//广告
            var _p_obj = {
                page_type: 3,//列表类型，1 广告类，2广告栏目，3广告
                pId: parentId,//上级ID
                pId1: add_Obj.pId1,
                province:'',//省
                city: '',//市
                district:''//区县
            };
            $(".mainDiv").load('htmlWeb/systemAdverListHtml', '', function () {
                page_Obj = _p_obj;
                $.getScript("../../by-webhtml/assets/js/system/system_adverList.js");
            });
        }else {//广告栏目
            var _p_obj = {
                page_type: 2,//列表类型，1 广告类，2广告栏目，3广告
                pId: parentId,//上级ID
                pId1: add_Obj.pId1,
                province:'',//省
                city: '',//市
                district:''//区县
            };
            $(".mainDiv").load('htmlWeb/systemAdverListHtml', '', function () {
                page_Obj = _p_obj;
                $.getScript("../../by-webhtml/assets/js/system/system_adverList.js");
            });
        }
    });
    $.ajax({
        url : "htmlWeb/adv/getOption",
        type : "post",
        dataType : "json",
        data : {id: id, parentId:parentId, advType:add_Obj.page_type, userId: loginUserId},
        success : function(data){
            if(loginUserType==5){
                var _byOpenCityList = "";
                if(data.byOpenCityList != undefined && data.byOpenCityList != null){
                    $.each(data.byOpenCityList, function (index, obj) {
                        _byOpenCityList += '<option value="' + obj.city + '">' + obj.city + '</option>';
                    });
                }
                $("#city").append(_byOpenCityList);
            }
            if (id != ''&& id != undefined) {
                $("#advForm").setValues(data.adv);
                $("#province").trigger("change");
                $("#city").val(data.adv.city);
                $("#city").trigger("change");
                $("#district").val(data.adv.district);
                $("#preview").attr("src","http://img.boyitongcheng.com/" + data.adv.imgUrl);
                ue.ready( function () {
                    ue.setHeight(420);
                    if(data.adv.descn){
                        ue.setContent(data.adv.descn);
                    }
                });
            }else {
                $("#parentId").val(parentId);
            }
        },
        error : function(){
            alert("查询error");
        }
    });
}
$(function () {
    getOption();
});